package vn.com.servicedirectory.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import vn.com.servicedirectory.common.CommonVariable;
import vn.com.servicedirectory.forms.ContactsEditAndCreateForm;
import vn.com.servicedirectory.forms.ContactsIndexForm;
import vn.com.servicedirectory.forms.ContactsSearch;

@Controller
public class ContactsController {
	@RequestMapping(value = CommonVariable.PAGE_CONTACTS_INDEX_HTML, method = RequestMethod.GET)
	public String showIndex(Model model) {
		ContactsIndexForm contactsIndexForm =new ContactsIndexForm();
		ContactsSearch contactsSearch =new ContactsSearch();
		contactsIndexForm.setFormTitle(CommonVariable.FORM_DEMO_TITLE);
		model.addAttribute("contactsIndex", contactsIndexForm);
		model.addAttribute("contactsSearch", contactsSearch);
		return CommonVariable.PAGE_CONTACTS_INDEX;
	}
	@RequestMapping(value = CommonVariable.PAGE_CONTACTS_INDEX_HTML, method = RequestMethod.POST)
	public ModelAndView serviceDeactive(@Valid ContactsSearch contactsSearch,
			Map<String, ContactsSearch> model, HttpServletRequest httpRequest) {
		model.put("serviceEditForm", contactsSearch);
		return new ModelAndView(CommonVariable.PAGE_CONTACTS_INDEX,
				"welcomeMessage", contactsSearch.getFirstName()
						+ httpRequest.getParameter("command"));

	}
	@RequestMapping(value = "contactsCreate")
	public ModelAndView contactsCreate() {
		return new ModelAndView(
				"redirect:contactsEditAndCreate.html?command=create");
		//return new ModelAndView(CommonVariable.PAGE_CONTACTS_INDEX,
		// "welcomeMessage",CommonVariable.CREATE);
	}
	@RequestMapping(value = CommonVariable.PAGE_CONTACTS_EDIT_UPDATE_HTML, method = RequestMethod.GET)
	public String showFormEditUpdate(Map<String, ContactsEditAndCreateForm> model,
			HttpServletRequest httpRequest) {
	/*	if(httpRequest.getParameter("command").equals("edit")){
			//To do edit
		}else{
			if(httpRequest.getParameter("command").equals("create")){
				//To do create
		}*/
		ContactsEditAndCreateForm contactsEditAndCreate = new ContactsEditAndCreateForm();
		model.put("contactsEditAndCreate", contactsEditAndCreate);
		return CommonVariable.PAGE_CONTACTS_EDIT_UPDATE;
	}
	@RequestMapping(value = "contactsEditBack")
	public String back(@Valid ContactsSearch contactsSearch,
			Map<String, ContactsSearch> model, HttpServletRequest httpRequest) {
		model.put("serviceEditForm", contactsSearch);
		return "redirect:" + CommonVariable.PAGE_CONTACTS_INDEX_HTML;
	}
	@RequestMapping(value = CommonVariable.PAGE_CONTACTS_EDIT_UPDATE_HTML, method = RequestMethod.POST)
	public ModelAndView serviceDeactive(@Valid ContactsEditAndCreateForm contactsEditAndCreate,
			Map<String, ContactsEditAndCreateForm> model, HttpServletRequest httpRequest) {
		model.put("contactsEditAndCreate", contactsEditAndCreate);
		return new ModelAndView(CommonVariable.PAGE_CONTACTS_EDIT_UPDATE,
				"welcomeMessage", httpRequest.getParameter("command"));

	}

}
